﻿using MvcApplication;
using MvcApplication.service;
using MvcEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication4MVC.request;

namespace WebApplication4MVC.Controllers
{
    public class LoginController : Controller
    {

        DemoService demoService = new DemoService();

        // GET: Login
        public ActionResult Index()
        {
            ViewBag.Title = "系统登录";
            return View();
        }

        public ActionResult DoLogin(LoginRequest request)
        {
            var reslt = new ResponseReslut();
            if (request == null)
            {
                return Json(new ResponseReslut() { code = 400, msg = "参数错误" });
            }

            if (string.IsNullOrWhiteSpace(request.UserName))
            {
                return Json(new ResponseReslut() { code = 400, msg = "用户名输入错误" });
            }

            if (string.IsNullOrWhiteSpace(request.UserPassword))
            {
                return Json(new ResponseReslut() { code = 400, msg = "用户密码输入错误" });
            }
            reslt = demoService.LoginForPwd(request.UserName, request.UserPassword);
            //HttpCookie httpCookie = new HttpCookie("LoginUserInfo");
            //httpCookie.Expires = DateTime.Now.AddMinutes(10); //DateTime.Now.AddDays(1);
            ////httpCookie.Expires = DateTime.Now.AddMinutes(-10);
            //httpCookie.Value = "1";
            if (reslt.code == 200)
            {
                DemoUser userinfo= reslt.data as DemoUser;
                UserInfoModel infoModel = new UserInfoModel {
                    Id=userinfo.Id,
                    UserName = userinfo.UserName,
                    UserPhone = userinfo.UserPhone,
                    LastDate = userinfo.LastDate.ToString("yyyy-MM-dd HH:mm:ss"),
                    CreateDate = userinfo.LastDate.ToString("yyyy-MM-dd HH:mm:ss"),
                    LoginAt = userinfo.LoginAt,
                };
                Session["LoginUserInfo"] = infoModel;
                reslt.data =new object();
            }
            



            return Json(reslt);
        }

        // GET: Login/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: Login/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Login/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Login/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: Login/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Login/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: Login/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
